import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class MenuView extends JPanel implements ActionListener,ChangeListener {

	private static final long serialVersionUID = 1L;
	private Image backgroundImage;
	private Image logoUtbm;
	private Image muteImage[] = new Image[5];
	private Image currentMuteImage;
	private Icon bgButton;
	private Icon bgButton2;

	private Boolean androidControleurStatus = false;
	private JButton androidControleur;
	private JSlider slider;
	private int sliderValue;

	private GameControler gameControleur;
	private SoundControler soundControler;

	private int currentMenu = 0; // 0 = Play, Return ... 1 = Hard, medium ... 2 = Level 1, level 2...

	private String[][] buttonsName = {{"Play", "Return to game", "Load game", "Save game", "Exit"},{"Hard", "Medium", "Easy", "Back to menu"}};
	private JButton[] arrayButton = new JButton[5];

	public MenuView(GameControler gameControleur, SoundControler soundControler) {
		this.gameControleur = gameControleur;
		this.soundControler = soundControler;
		this.sliderValue = (int) (soundControler.getOutputVolume()*100);
		this.loadRessources();
		this.setLayout(null);
	}

	/**
	 * Charge les ressources neccessaire.
	 */
	private void loadRessources() {
		try {
			java.net.URL url = ClassLoader.getSystemClassLoader().getResource("Ressources/button_bg.png");
			this.bgButton = new ImageIcon(url);
			url = ClassLoader.getSystemClassLoader().getResource("Ressources/button_bg2.png");
			this.bgButton2 = new ImageIcon(url);
			this.backgroundImage = ImageIO.read((getClass().getResource("Ressources/bg-clouds.png")));
			this.logoUtbm = ImageIO.read((getClass().getResource("Ressources/utbm.png")));
			for(int i=0; i< muteImage.length ; ++i)
				this.muteImage[i] = ImageIO.read((getClass().getResource("Ressources/muteIcon"+i+".png")));
			this.currentMuteImage = muteImage[3];
		} catch (IOException e) {e.printStackTrace();}
	}

	/**
	 * Permet de switcher de menu
	 * @param a
	 */
	public void switchToMenu(int a){
		this.currentMenu = a;
		this.loadMenu(a);
	}

	/**
	 * Permet de charger un menu ( 1 ou 0 )
	 * @param menu
	 */
	private void loadMenu(int menu) {
		this.removeAll();
		this.arrayButton = new JButton[5];
		int cpt = 0;
		for(int i=0; i< this.buttonsName[menu].length; ++i) {
			
			this.arrayButton[i] = new JButton(buttonsName[menu][i],this.bgButton);
			this.arrayButton[i].setRolloverIcon(this.bgButton2);
			this.arrayButton[i].setHorizontalTextPosition(SwingConstants.CENTER);
			this.arrayButton[i].setBackground(new Color(0,0,0,0));
			this.arrayButton[i].setOpaque(true);
			this.arrayButton[i].setBorderPainted(false);

			if(this.buttonsName[menu][i].startsWith("Load game")) {
				this.arrayButton[i].setBounds((Windows.WIDTH/2)-150, 80+(i*80), 150, 70);
				++cpt;
			}
			else if(this.buttonsName[menu][i].startsWith("Save game")){
				this.arrayButton[i].setBounds((Windows.WIDTH/2)+5, 80+((i-1)*80), 150, 70);
				cpt = 1;
			}
			else
				this.arrayButton[i].setBounds((Windows.WIDTH/2)-75, 80+((i-cpt)*80), 150, 70);
			this.arrayButton[i].addActionListener(this);
			this.add(this.arrayButton[i]);
		}

		this.loadSideWidgets();
		if(menu == 0) 
			this.arrayButton[1].setEnabled(gameControleur.hasLevelRunning());

	}

	/**
	 * Permet de charger le menu des niveaux.
	 * @param levelMax
	 * @param difficulty
	 */
	public void loadLevelMenu(int levelMax, int difficulty, int scores[]) {
		this.currentMenu = 2;
		this.removeAll();
		int nbOfLevels = 0;
		if(difficulty == 1)
			nbOfLevels = Map.NUMBER_OF_LEVEL_EASY;
		else if(difficulty == 2) 
			nbOfLevels = Map.NUMBER_OF_LEVEL_MEDIUM;
		else if (difficulty == 3)
			nbOfLevels = Map.NUMBER_OF_LEVEL_HARD;

		int cpt = 0;
		int posX = 0;
		this.arrayButton = new JButton[nbOfLevels+1];
		for(int i=0; i< nbOfLevels; ++i) {
			this.arrayButton[i] = new JButton(String.valueOf(i+1)+" | score : "+scores[i],this.bgButton);	
			this.arrayButton[i].setBounds((cpt*170)+(Windows.WIDTH/2)-75, 90+(posX*80), 150, 70);
			this.arrayButton[i].addActionListener(this);
			this.arrayButton[i].setRolloverIcon(this.bgButton2);
			this.arrayButton[i].setHorizontalTextPosition(SwingConstants.CENTER);
			this.arrayButton[i].setBackground(new Color(0,0,0,0));
			this.arrayButton[i].setOpaque(true);
			this.arrayButton[i].setBorderPainted(false);
			if(i >= levelMax)
				this.arrayButton[i].setEnabled(false);
			this.add(this.arrayButton[i]);

			if(i != 0 && (i+1)%3 == 0){
				++cpt;
				posX = 0;
			}
			else
				++posX;
		}

		this.arrayButton[cpt] = new JButton("Change Difficulty",this.bgButton);	
		this.arrayButton[cpt].setBounds((Windows.WIDTH/2)-240,250, 150, 70);
		this.arrayButton[cpt].addActionListener(this);
		this.arrayButton[cpt].setRolloverIcon(this.bgButton2);
		this.arrayButton[cpt].setHorizontalTextPosition(SwingConstants.CENTER);
		this.arrayButton[cpt].setBackground(new Color(0,0,0,0));
		this.arrayButton[cpt].setOpaque(true);
		this.arrayButton[cpt].setBorderPainted(false);
		this.add(this.arrayButton[cpt]);
		this.loadSideWidgets();
	}

	private void loadSideWidgets(){

		this.slider = new JSlider(JSlider.VERTICAL, 0, 100, this.sliderValue);
		this.slider.setBounds(907, 120, 20, 200);
		this.slider.addChangeListener(this);
		this.add(slider);

		this.androidControleur = new JButton();
		this.switchAndroidImageIcon(this.androidControleurStatus);
		this.androidControleur.setBounds(885, 320, 60, 60);
		this.androidControleur.addActionListener(this);
		this.androidControleur.setBackground(new Color(0,0,0,0));
		this.androidControleur.setOpaque(true);
		this.androidControleur.setBorderPainted(false);
		this.add(androidControleur);

	}

	private void switchAndroidImageIcon(Boolean stat){
		java.net.URL url;
		if(!stat) {
			url = URL.class.getResource("/Ressources/androidbuttonNO.png");
			this.androidControleurStatus = false;
		}
		else {
			url = URL.class.getResource("/Ressources/androidbutton.png");
			this.androidControleurStatus = true;
		}
		this.androidControleur.setIcon(new ImageIcon(url));
		this.repaint();
	}

	public void paintComponent(Graphics g) {
		this.drawBackground(g);
		this.drawUI(g);
	}

	private void drawBackground(Graphics g) {
		g.setColor(new Color(0x8e, 0xc1, 0xda));
		g.fillRect(0, 0, Windows.WIDTH, Windows.HEIGHT);
		g.drawImage(this.backgroundImage,(int) Windows.position_bg, 0, 2247, 190, null);
		g.drawImage(this.logoUtbm, -110, 0,400,163,null);


		Windows.position_bg -= 0.5;
		if(Windows.position_bg < -1210){
			Windows.position_bg = -646;
		}

	}

	private void drawUI(Graphics g) {
		g.drawImage(currentMuteImage, 900,70,56,56,null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.soundControler.playFX("menu_click.wav");
		if(e.getSource() == this.androidControleur) {
			this.switchAndroidImageIcon(!this.androidControleurStatus);
			if(this.androidControleurStatus) 
				this.gameControleur.startAndroidWifiConnection();
			else 
				this.gameControleur.killAndroidWifiConnection();
		}
		else if(this.currentMenu == 0) {
			if(e.getSource() == arrayButton[0]) {
				this.switchToMenu(1);
			}
			else if(e.getSource() == arrayButton[1]) {
				this.gameControleur.loadCurrentGame();
			}
			else if(e.getSource() == arrayButton[2]) {
				this.gameControleur.loadGameFromFile();
			}
			else if(e.getSource() == arrayButton[3]) {
				this.gameControleur.saveGameToFile();
			}
			else if(e.getSource() == arrayButton[4]) {
				this.gameControleur.exitGame();
			}
		}
		else if(this.currentMenu == 1) {
			if(e.getSource() == arrayButton[0]) {
				this.gameControleur.loadLevelMenu(GameControler.DIFFICULTY_HARD);
			}
			if(e.getSource() == arrayButton[1]) {
				this.gameControleur.loadLevelMenu(GameControler.DIFFICULTY_MEDIUM);
			}
			if(e.getSource() == arrayButton[2]) {
				this.gameControleur.loadLevelMenu(GameControler.DIFFICULTY_EASY);
			}
			if(e.getSource() == arrayButton[3]) {
				this.switchToMenu(0);
			}
		}
		else if(this.currentMenu == 2) {
			JButton aux = (JButton) e.getSource();
			String text = aux.getText();
			try { 
				String a = String.valueOf(text.charAt(0));
				int b = Integer.parseInt(a);
				this.gameControleur.loadNewGame(b);
			}
			catch(NumberFormatException e1) {
				this.switchToMenu(1);
			};
		}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == this.slider) {
			int val = this.slider.getValue();
			this.sliderValue = val;
			if(val == 0)
				currentMuteImage = muteImage[0];
			else if(val < 25)
				currentMuteImage = muteImage[1];
			else if(val < 50)
				currentMuteImage = muteImage[2];
			else if(val < 75)
				currentMuteImage = muteImage[3];
			else
				currentMuteImage = muteImage[4];

			this.soundControler.setOutputVolume((double)val/100);
		}
	}
}
